package com.ruoyi.medical.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.medical.domain.MedicineOrders;
import com.ruoyi.medical.domain.OrderVo;
import com.ruoyi.medical.domain.OrdersDetail;
import com.ruoyi.medical.mapper.MedicineOrdersMapper;
import com.ruoyi.medical.mapper.OrdersDetailMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.medical.mapper.OrdersMapper;
import com.ruoyi.medical.domain.Orders;
import com.ruoyi.medical.service.IOrdersService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 药品订单管理Service业务层处理
 *
 * @author WGH
 * @date 2025-08-23
 */
@Service
public class OrdersServiceImpl implements IOrdersService {
    @Autowired
    private OrdersMapper ordersMapper;
    @Resource
    private MedicineOrdersMapper medicineOrdersMapper;
    @Resource
    private OrdersDetailMapper ordersDetailMapper;

    /**
     * 查询药品订单管理
     *
     * @param id 药品订单管理主键
     * @return 药品订单管理
     */
    @Override
    public Orders selectOrdersById(Long id) {
        return ordersMapper.selectOrdersById(id);
    }

    /**
     * 查询药品订单管理列表
     *
     * @param orders 药品订单管理
     * @return 药品订单管理
     */
    @Override
    public List<Orders> selectOrdersList(Orders orders) {
        return ordersMapper.selectOrdersList(orders);
    }

    /**
     * 新增药品订单管理
     *
     * @param orders 药品订单管理
     * @return 结果
     */
    @Override
    public int insertOrders(Orders orders) {

        return ordersMapper.insertOrders(orders);
    }

    /**
     * 修改药品订单管理
     *
     * @param orders 药品订单管理
     * @return 结果
     */
    @Override
    public int updateOrders(Orders orders) {
        return ordersMapper.updateOrders(orders);
    }

    /**
     * 批量删除药品订单管理
     *
     * @param ids 需要删除的药品订单管理主键
     * @return 结果
     */
    @Override
    public int deleteOrdersByIds(Long[] ids) {
        return ordersMapper.deleteOrdersByIds(ids);
    }

    /**
     * 删除药品订单管理信息
     *
     * @param id 药品订单管理主键
     * @return 结果
     */
    @Override
    public int deleteOrdersById(Long id) {
        return ordersMapper.deleteOrdersById(id);
    }

    @Override
    // 添加订单信息
    @Transactional
    public int insertBatchOrders(List<OrderVo> orders) {
        // 先向订单表添加数据    获取订单id
        Orders orders1 = new Orders();

        orders1.setPatientId(orders.get(0).getPatientId());
        orders1.setDel(0L);
        int i = ordersMapper.insertBatchOrders(orders1);
        Long id = orders1.getId(); // 订单的id

        // 向订单药品中间表添加数据
        ArrayList<MedicineOrders> list = new ArrayList<>(); // 订单列表
        for (OrderVo order : orders) {
            MedicineOrders medicineOrders = new MedicineOrders();
            medicineOrders.setOrdersId(id);
            medicineOrders.setMedicineId(order.getMedicineId());
            medicineOrders.setCount(order.getCount());
            medicineOrders.setPrice(order.getPrice());
            medicineOrders.setDel(0L);
            list.add(medicineOrders);
        }
        list.forEach(l -> {
            System.out.println(l);
        });
        i *= medicineOrdersMapper.insertBatchMedicineOrders(list);

        // 向订单详情表添加数据
        OrdersDetail detail = new OrdersDetail();
        detail.setOrdersId(id);
        Double totlePrice = 0.0;
        for (int j = 0; j < orders.size(); j++) {
            totlePrice += orders.get(j).getTotlePrice();
        }
        detail.setTotlePrice(totlePrice);
        detail.setDel(0L);
        i*=ordersDetailMapper.insertOrdersDetail(detail);

        return i;
    }
}
